package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayLouKa;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinLouKaService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_楼卡_小程序接口
 */

@Controller
@RequestMapping("/sunday/small/louka")
@Api(value = "louka",description = "楼卡")
public class SundaySmallLouKaController extends BaseController {
    @Autowired
    private SundayWeiXinLouKaService loukaService;

    @ApiOperation(value = "【小程序】获取楼卡list")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public void select(HttpServletRequest request,
                       HttpServletResponse response) {
        try {
            List<SundayLouKa> result = loukaService.selectLouka(request);
            ResponseMessage.success(result);
        } catch (Exception e) {
            ResponseMessage.error(CommonConstants.FAIL_CODE, "error");
        }
    }

    @ApiOperation("【小程序】用户获取楼卡详情")
    @RequestMapping(value = "/getDetail",method = RequestMethod.POST)
    public void getDetail(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(value = "楼卡id",required = true)@RequestParam(value = "loukaId")Long loukaId
    ){
        try {
            ResponseMessage.success(loukaService.findOne(loukaId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/louka/getDetail---e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

}
